import {httpGet, httpPost} from "../index";
import {setLoginToken} from "../../utils/db";
import {Toast} from "vant";
import config from "../../config";
import {navigateTo} from "../../utils/navigate";
import {isString} from "../../utils/helpers";



/**
 * 用户注册
 * @param params
 * @returns {Promise<*>}
 */
export async function userRegister(params = {}) {
    let res = await httpPost('user/register', params);
    if (res.message) {
        Toast(res.message)
    }
    return res.state === 200;
}

/**
 * 用户登录
 * @param params
 * @returns {Promise<*>}
 */
export async function login(params = {}) {
    let res = await httpPost('auth/login', params);
    if (res.state === 200) {
        setLoginToken(res.data.token);
    }
    return res;
}


export async function logout(params = {}, message) {
    await httpGet('auth/logout', params);
    if (!isString(message)) {
        message = '退出成功';
    }
    if (message.length) Toast(message);
    navigateTo(config.loginRouteName);
    return true;
}

/**
 * 获取用户信息
 * @param params
 * @returns {Promise<*>}
 */
export async function userInfo(params = {}) {
    return httpGet('user/info', params);

}


/**
 * 用户密码修改
 * @param params
 * @returns {Promise<*>}
 */
export async function userChangePass(params = {}) {
    return httpPost('user/changePass', params);

}


